package com.aishua.fire.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextUtil implements ApplicationContextAware {

    /**
     * 实现ApplicationContextAware接口的回调方法，设置上下文环境
     *
     * @param applicationContext
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
    	SharedVariable.setApplicationContext(applicationContext);
    }

    public static ApplicationContext getApplicationContext() {
        return SharedVariable.getApplicationContext();
    }

    /**
     * 获取对象
     *
     * @param name
     * @return 一个以所给名字注册的bean的实例 (service注解方式，自动生成以首字母小写的类名为bean name)
     * @throws BeansException
     */
    public static Object getBean(String name) throws BeansException {
        return SharedVariable.getApplicationContext().getBean(name);
    }

    public static <T> T getBeanByType(Class<T> requiredType) {
        return SharedVariable.getApplicationContext().getBean(requiredType);
    }

}
